﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.IO;

namespace Acsys
{
    public class RepositoryFactory
    {
        Crypto m_crypto = new Crypto();
        Hashtable m_listService = new Hashtable();
        static volatile RepositoryFactory m_instance;
        const string USER_REPOSITORY = "UserRepository";
        const string CREDIT_TYPE_REPOSITORY = "CreditTypeRepository";
        const string DR_TR_RPT_REPOSITORY = "DrTrRptRepository";
        const string CR_TR_RPT_REPOSITORY = "CrTrRptRepository";
        const string DEBIT_TYPE_REPOSITORY = "DebitTypeRepository";
        const string TRANSACTION_REPOSITORY = "TransactionRepository";
        const string DEBIT_TRANSACTION_REPOSITORY = "DebitTransactionRepository";
        const string CREDIT_TRANSACTION_REPOSITORY = "CreditTransactionRepository";
        MySqlConnection m_connection;// = new MySqlConnection(ConfigurationSettings.AppSettings["ConnectionDriver"] + ";database=acsys_db;uid=root;pwd=1234");

        public static RepositoryFactory GetInstance()
        {
            if (m_instance == null)
            {
                m_instance = new RepositoryFactory();
               
            }
            return m_instance;
        }
        public void setConnectionString(string cn)
        {
            try
            {
                m_connection = new MySqlConnection(cn);
                m_connection.Open();
            }
            catch (Exception x)
            {
                throw new Exception("Please setting the Configuration Connection");
            }
        }
        public RepositoryFactory()
        {
            
            if (File.Exists("setting.conf"))
            {
                using (StreamReader file = new StreamReader(@"setting.conf"))
                {
                    //string connection = "Driver={" + m_crypto.Decrypt(file.ReadLine()) + "};server=" + m_crypto.Decrypt(file.ReadLine()) + ";database=acsys_db;uid=" + m_crypto.Decrypt(file.ReadLine()) + ";pwd=" + m_crypto.Decrypt(file.ReadLine()) + "";
                    string driver = m_crypto.Decrypt(file.ReadLine());
                    string connection = "server=" + m_crypto.Decrypt(file.ReadLine()) + ";database=acsys_db;uid=" + m_crypto.Decrypt(file.ReadLine()) + ";pwd=" + m_crypto.Decrypt(file.ReadLine()) + "";
                    m_connection = new MySqlConnection(connection);
                }
            }
            else
            {
                throw new Exception("Please setting the Configuration Connection");
            }
            Repository userRepository = new Repository();
            CreditTypeRepository creditTypeRepository = new CreditTypeRepository();
            DrTrRptRepository drTrReportRepository = new DrTrRptRepository();
            CrTrRptRepository crTrReportRepository = new CrTrRptRepository();
            DebitTypeRepository debitTypeRepository = new DebitTypeRepository();
            TransactionRepository trRepository = new TransactionRepository();
            DebitTransactionRepository drtrRepository = new DebitTransactionRepository();
            CreditTransactionRepository crtrRepository = new CreditTransactionRepository();

            userRepository.SetConnection(m_connection);
            drTrReportRepository.SetConnection(m_connection);
            crTrReportRepository.SetConnection(m_connection);
            creditTypeRepository.SetConnection(m_connection);
            debitTypeRepository.SetConnection(m_connection);
            trRepository.SetConnection(m_connection);
            drtrRepository.SetConnection(m_connection);
            crtrRepository.SetConnection(m_connection);

            m_listService.Add(USER_REPOSITORY, userRepository);
            m_listService.Add(DR_TR_RPT_REPOSITORY, drTrReportRepository);
            m_listService.Add(CR_TR_RPT_REPOSITORY,crTrReportRepository);
            m_listService.Add(CREDIT_TYPE_REPOSITORY, creditTypeRepository);
            m_listService.Add(DEBIT_TYPE_REPOSITORY, debitTypeRepository);
            m_listService.Add(TRANSACTION_REPOSITORY, trRepository);
            m_listService.Add(DEBIT_TRANSACTION_REPOSITORY, drtrRepository);
            m_listService.Add(CREDIT_TRANSACTION_REPOSITORY, crtrRepository);


        }
        public Repository CodeNameRepository()
        {
            return m_listService[USER_REPOSITORY] as Repository;
        }
        public DrTrRptRepository DrTrReportRepository()
        {
            return m_listService[DR_TR_RPT_REPOSITORY] as DrTrRptRepository;
        }
        public CrTrRptRepository CrTrReportRepository()
        {
            return m_listService[CR_TR_RPT_REPOSITORY] as CrTrRptRepository;
        }
        public CreditTypeRepository CreditTypeRepository()
        {
            return m_listService[CREDIT_TYPE_REPOSITORY] as CreditTypeRepository;
        }
        public DebitTypeRepository DebitTypeRepository()
        {
            return m_listService[DEBIT_TYPE_REPOSITORY] as DebitTypeRepository;
        }
        public TransactionRepository TransactionRepository()
        {
            return m_listService[TRANSACTION_REPOSITORY] as TransactionRepository;
        }
        public DebitTransactionRepository DebitTransactionRepository()
        {
            return m_listService[DEBIT_TRANSACTION_REPOSITORY] as DebitTransactionRepository;
        }
        public CreditTransactionRepository CreditTransactionRepository()
        {
            return m_listService[CREDIT_TRANSACTION_REPOSITORY] as CreditTransactionRepository;
        }
    }
}
